/*
 * XRowHeaderRenderer.java
 * 
 * Copyright (c) 2006 - 2007
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.xfc.table;

import java.awt.Color;
import java.awt.Component;
import java.text.NumberFormat;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.border.BevelBorder;
import javax.swing.border.Border;
import javax.swing.table.TableCellRenderer;

/**
 * 
 * 
 * @author Devon Carew
 */
public class XRowHeaderRenderer
	extends JLabel
	implements TableCellRenderer
{
	private boolean renderLineNumber;
	
	private Color defaultBackground;
	private Color selectedBackground;
	
	private NumberFormat nf = NumberFormat.getIntegerInstance();
	
	public XRowHeaderRenderer()
	{
		this(false);
	}
	
	public XRowHeaderRenderer(boolean renderLineNumber)
	{
		this.renderLineNumber = renderLineNumber;
		
        // TODO: use TableHeaderUI?
        defaultBackground = UIManager.getColor("TableHeader.background");
        selectedBackground = UIManager.getColor("Table.selectionBackground");
		
		setOpaque(true);
		
		setHorizontalAlignment(JLabel.RIGHT);
		
        Border border = UIManager.getBorder("TableHeader.cellBorder");
        
        if (border == null)
        {
            Color controlLight = UIManager.getColor("controlLtHighlight");
            Color controlDark = UIManager.getColor("controlDkShadow");
            
            border = BorderFactory.createBevelBorder(BevelBorder.RAISED, controlLight, controlDark);
        }
        
        setBorder(BorderFactory.createCompoundBorder(
            border, BorderFactory.createEmptyBorder(0, 3, 0, 3)));
	}
	
	public Component getTableCellRendererComponent(JTable table,
		Object value, boolean isSelected, boolean hasFocus, int rowIndex, int columnIndex)
	{
		if (renderLineNumber)
			setText(nf.format(rowIndex + 1));
		else
			setText(" ");
		
		setBackground(isSelected ? selectedBackground : defaultBackground);
		
		return this;
	}
    
}
